package org.mindrive.system.web.system;

import java.util.Map;

import org.mindrive.system.annotation.LogOperation;
import org.mindrive.system.core.Result;
import org.mindrive.system.parameter.system.UploadContinueMergeParameter;
import org.mindrive.system.parameter.system.UploadToTempParameter;
import org.mindrive.system.service.BaseService;
import org.mindrive.system.service.system.UploadService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Api(tags = { "断点续传" })
@RestController
@RequestMapping("/upload")
@Slf4j
public class UploadController {
	@Autowired
	UploadService uploadCommonService;

	@LogOperation(type = BaseService.AUDIT_EVENT_TYPE_IMPORT, event = "将分片文件上传")
	@ApiOperation(value = "将分片文件上传", notes = "将分片文件上传")
	@ApiImplicitParam(paramType = "header", dataType = "String", name = "authorization", value = "令牌", required = true)
	@PostMapping(path = "/upload-to-temp")
	public Result bigFile(@RequestParam(value = "file") MultipartFile file,
			@Validated UploadToTempParameter parameter) {
		log.info("分片文件上传开始。[{}]", JSON.toJSONString(parameter));
		Result result = new Result();
		try {
			uploadCommonService.bigFile(file, parameter);
			result = Result.ok();
		} catch (Exception ex) {
			log.error("分片文件上传失败。", ex);
			result = Result.error(ex.getMessage());
		}
		log.info("分片文件上传结束。");
		return result;
	}

	@LogOperation(type = BaseService.AUDIT_EVENT_TYPE_SEARCH, event = "检查是否存在已上传部分")
	@ApiOperation(value = "检查是否存在已上传部分", notes = "检查是否存在已上传部分")
	@ApiImplicitParam(paramType = "header", dataType = "String", name = "authorization", value = "令牌", required = true)
	@PostMapping(path = "/checkshard")
	public Result checkshard(@RequestBody String filemd5) {
		log.info("检查是否存在已上传部分开始。[{}]", filemd5);
		Result result = new Result();
		try {
			Map<String, Object> map = uploadCommonService.checkshard(filemd5);
			result = Result.ok(map);
		} catch (Exception ex) {

			log.error("检查是否存在已上传部分失败。", ex);
			result = Result.error(ex.getMessage());
		}
		log.info("检查是否存在已上传部分结束。");
		return result;
	}

	@LogOperation(type = BaseService.AUDIT_EVENT_TYPE_MODIFY, event = "将分片文件合并")
	@ApiOperation(value = "将分片文件合并", notes = "将分片文件合并")
	@ApiImplicitParam(paramType = "header", dataType = "String", name = "authorization", value = "令牌", required = true)
	@PostMapping(path = "/merge")
	public Result mergeFile(@RequestBody UploadContinueMergeParameter parameter) {
		log.info("将分片文件合并开始。[{}]", JSON.toJSONString(parameter));
		Result result = new Result();
		try {
			String downloadId = uploadCommonService.mergeFile(parameter);
			result = Result.ok(downloadId);
		} catch (Exception ex) {
			log.error("将分片文件合并失败。", ex);
			result = Result.error(ex.getMessage());
		}
		log.info("将分片文件合并结束。");
		return result;
	}

	@LogOperation(type = BaseService.AUDIT_EVENT_TYPE_MODIFY, event = "取消上传")
	@ApiOperation(value = "取消上传", notes = "取消上传")
	@ApiImplicitParam(paramType = "header", dataType = "String", name = "authorization", value = "令牌", required = true)
	@PostMapping(path = "/upload-cancel")
	public Result uploadCancel(@RequestBody UploadContinueMergeParameter parameter) {
		log.info("取消上传开始。[{}]", JSON.toJSONString(parameter));
		Result result = new Result();
		try {
			uploadCommonService.uploadCancel(parameter);
			result = Result.ok();
		} catch (Exception ex) {
			log.error("取消上传失败。", ex);
			result = Result.error(ex.getMessage());
		}
		log.info("取消上传结束。");
		return result;
	}
}
